// 
// Decompiled by Procyon v0.5.36
// 

package com.orange.boot.dev1.service.impl;

import org.slf4j.LoggerFactory;
import com.orange.boot.dev1.entity.DetailUrl;
import java.util.Iterator;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import org.jsoup.Jsoup;
import com.orange.boot.dev1.entity.PaChongNew;
import java.util.ArrayList;
import com.orange.boot.utils.ResultCode;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.orange.boot.utils.R;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import com.orange.boot.dev1.service.NewsService;
import com.orange.boot.dev1.entity.News;
import com.orange.boot.dev1.mapper.NewsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    private static final Logger log = LoggerFactory.getLogger(NewsServiceImpl.class);

    public NewsServiceImpl() {
    }

    public R getNews(int count) {
        List<News> news = ((NewsMapper)this.baseMapper).selectList((Wrapper)((QueryWrapper)(new QueryWrapper()).orderByDesc("gmt_create")).last("limit " + count));
        return R.ok().data("news", news);
    }

    public R paChong(Integer page) {
        if (page > 9) {
            return R.error().code(ResultCode.BEYOND_PAGES).message("超过总页数！");
        } else {
            List<PaChongNew> list = new ArrayList();
            String url = "http://www.ganjuren.com/jishu/bchgl/p10" + page.toString() + ".html";
            Document document = null;

            try {
                document = Jsoup.connect(url).get();
            } catch (IOException var14) {
                var14.printStackTrace();
                return R.error().message("爬取信息失败！");
            }

            Elements elements = document.select("ul.zs-list > li");
            Iterator var6 = elements.iterator();

            while(var6.hasNext()) {
                Element element = (Element)var6.next();
                PaChongNew eachNew = new PaChongNew();
                Element img = element.selectFirst("img");
                String title;
                if (img != null) {
                    title = img.attr("src");
                    String[] split = title.split("/");
                    StringBuilder picUrl = (new StringBuilder()).append("http://www.ganjuren.com/uploads/allimg/").append(split[split.length - 2]).append("/").append(split[split.length - 1]);
                    eachNew.setPicUrl(picUrl);
                }

                title = element.selectFirst("strong").child(0).text();
                eachNew.setTitle(title);
                String detailUrl = "http://www.ganjuren.com/" + element.selectFirst("strong").child(0).attr("href");
                eachNew.setDetailUrl(detailUrl);
                String introduction = element.selectFirst("p").text();
                eachNew.setIntroduction(introduction);
                String publishTime = element.selectFirst("span").text();
                eachNew.setPublishTime(publishTime);
                list.add(eachNew);
            }

            return R.ok().data("list", list);
        }
    }

    public R detail(DetailUrl detailUrl) {
        try {
            Document document = Jsoup.connect(detailUrl.getDetailUrl()).get();
            Elements elements = document.getElementsByClass("d-art");
            String html = ((Element)elements.get(0)).toString();
            System.out.println("----------------------");
            System.out.println(html);
            boolean isEnd = false;

            for(int nextSrc = 0; !isEnd; isEnd = nextSrc == -1) {
                int srcIndex = html.indexOf("src", nextSrc);
                int srcBegin = html.indexOf("\"", srcIndex);
                int srcEnd = html.indexOf("\"", srcBegin + 1);
                String srcContent = html.substring(srcBegin + 1, srcEnd);
                String picUrl = "http://www.ganjuren.com" + srcContent;
                html = html.substring(0, srcBegin + 1) + picUrl + html.substring(srcEnd);
                nextSrc = html.indexOf("src", html.indexOf("\"", srcBegin + 1));
            }

            System.out.println("--------------------------------");
            System.out.println(html);
            return R.ok().data("htmlText", html);
        } catch (Exception var12) {
            var12.printStackTrace();
            return R.error().message("爬取页面失败！");
        }
    }

    public R newPaChong(Integer page) throws IOException {
        return null;
    }
}
